package cn.lin.controller;

import cn.lin.domain.Catalog;
import cn.lin.service.CatalogService;
import cn.lin.util.Result;
import cn.lin.util.UploadVideoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author qmp
 * @date 2024/12/5 15:18
 */
@RestController
@CrossOrigin
@RequestMapping("/upload")
public class VideoUploadController {

    @Autowired
    private CatalogService catalogService ;

    @RequestMapping("/video")
    public Object uploadVideo(@RequestParam("file") MultipartFile file,@RequestParam("classId") Integer classId,@RequestParam("pid") Integer pid) {

        try {
            String url = UploadVideoUtil.uploadVideo(file);

            String fileName = file.getOriginalFilename();
            Date date = new Date() ;
            Catalog catalog = new Catalog() ;
            catalog.setModelName(fileName);
            catalog.setPid(pid);
            catalog.setClassId(classId);
            catalog.setCreatedDate(date);
            catalog.setFileName(url);
            catalog.setLevel(3);
            int i = catalogService.addCatalog(catalog);

            if (i != 0){
                return Result.ok("上传成功",url) ;
            }else {
                return Result.error("上传成功，但数据库异常导致插入失败") ;
            }
        } catch (Exception e) {
            return Result.error(e.getMessage()) ;
        }


    }

}
